package org.dy.servlet.listener;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/*
 * check if the whole war is configured right
 * so you can use dytemplate to run a project
 */
public class InitCheckListener implements ServletContextListener {

	public void contextDestroyed(ServletContextEvent ev) {

	}

	public void contextInitialized(ServletContextEvent ev) {
		ServletContext context = ev.getServletContext();
		
		String configFileDir = context.getRealPath("WEB-INF");
		String classPathDir = context.getRealPath("WEB-INF/classes");
		String dependLibDir = context.getRealPath("WEB-INF/lib");
		
		String crudConfigDir = context.getRealPath("crud");
		
		isConfigFileOk(configFileDir);
		isConfigFileTwoOk(classPathDir);
		isLibDependOk(dependLibDir);
		isCrudConfigOk(crudConfigDir);
	}
	
	private boolean contain(List<String> list, String target, String msg){
		return contain(list, target, msg, true);
	}
	
	private boolean contain(List<String> list, String target, String msg, boolean canIgnore){
		target = target.toLowerCase();
		if(list.contains(target)){
			System.out.println("Config " + msg + " Ok");
			return true;
		}else{
			System.out.println("Config " + msg + (canIgnore?" Ignore":" Failed, Check Again"));
			return false;
		}
	}
	
	protected boolean isConfigFileOk(String str){
		File file = new File(str);
		if(file.exists() && file.canRead() && file.isDirectory()){
			File[] subFileList = file.listFiles();
			List<String> fileNameList = new ArrayList<String>();
			for(File subFile:subFileList){
				fileNameList.add(subFile.getName().trim().toLowerCase());
			}
			
			System.out.println("Config File Directory Find as 'WEB-INF/'");
			
			contain(fileNameList, "i18n", "I18N");
			contain(fileNameList, "action_mapping.properties", "Url Mapping");
			contain(fileNameList, "ftl.properties", "Freemarker Configuration");
		}else{
			System.out.println("Configure Files' Directory 'WEB-INF/class' Missing");
		}
		return true;
	}
	
	protected boolean isConfigFileTwoOk(String str){
		File file = new File(str);
		if(file.exists() && file.canRead() && file.isDirectory()){
			File[] subFileList = file.listFiles();
			List<String> fileNameList = new ArrayList<String>();
			for(File subFile:subFileList){
				fileNameList.add(subFile.getName().trim().toLowerCase());
			}
			
			System.out.println("Config File Directory Find as 'WEB-INF/'");
			
			contain(fileNameList, "log4j.properties", "Log4j");
			contain(fileNameList, "oscache.properties", "Oscache In DAO");
			contain(fileNameList, "view_mapping.properties", "View Redirect");
		}else{
			System.out.println("Configure Files' Directory 'WEB-INF/class' Missing");
		}
		return true;
	}
	
	protected boolean isLibDependOk(String str){
		File file = new File(str);
		if(file.exists() && file.canRead() && file.isDirectory()){
			File[] subFileList = file.listFiles();
			List<String> fileNameList = new ArrayList<String>();
			for(File subFile:subFileList){
				fileNameList.add(subFile.getName());
			}
			
			System.out.println("Depend Jar File Directory Find as 'WEB-INF/lib'");
			
			contain(fileNameList, "commons-fileupload.jar", "Upload Module");
			contain(fileNameList, "commons-io.jar", "Upload Module");
			contain(fileNameList, "commons-logging.jar", "Log Module");
			
			contain(fileNameList, "json-lib.jar", "Json Module");
			
			contain(fileNameList, "jdom.jar", "Crud Module", false);
			contain(fileNameList, "jdom.jar", "Freemarer Adaptor Module");
		}else{
			System.out.println("Configure Files' Directory 'WEB-INF/class' Missing");
		}
		return true;
	}
	
	protected boolean isCrudConfigOk(String str){
		File file = new File(str);
		if(file.exists() && file.canRead() && file.isDirectory()){
			System.out.println("Crud Module Ok, Copy Files under 'ftl' Directory to 'WEB-INF/ftl/crud/");
			System.out.println("Crud Module Ok, Before Using, Config 'config' Files Refering 'dytemplate/doc/crud'");
		}
		return true;
	}
	
}
